﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 测试
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private const float source_x = 600, source_y = 540;
        private const float source_v = 20;
        private const float target_x = 0, target_y = 0;
        private const float target_v = 50;

        private float obj_width = 5;
        private float obj_height = 5;
        MyVector sourceObj = new MyVector(source_x, source_y, source_v, Color.Red);
        MyVector targetObj = new MyVector(target_x, target_y, target_v, Color.Green);

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            PaintObj(sourceObj);
            PaintObj(targetObj);
        }

        /// <summary>
        /// 根据向量画图
        /// </summary>
        /// <param name="vector"></param>
        /// <param name="color"></param>
        private void PaintObj(MyVector vector)
        {
            using (Brush b = new SolidBrush(vector.MyColor))
            {
                RectangleF rect = new RectangleF(vector.X, vector.Y, obj_width, obj_height);
                Graphics g = this.panel1.CreateGraphics();
                g.FillRectangle(b, rect);
            }
        }

        /// <summary>
        /// 重绘
        /// </summary>
        /// <param name="vector"></param>
        /// <param name="color"></param>
        private void RePaintObj(MyVector oldRector, MyVector newRector)
        {
            PaintObj(oldRector); //删除旧的
            PaintObj(newRector);//重画新的
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            RePaintObj(new MyVector(sourceObj.X, sourceObj.Y, sourceObj.Rete, Color.Black),
                VectorHepler.GetNewVector(sourceObj, targetObj));
        }

        private void AMove(Keys keyData)
        {
            float x = targetObj.X;
            float y = targetObj.Y;
            switch (keyData)
            {
                case Keys.Right:
                    targetObj.X += targetObj.Rete;
                    break;
                case Keys.Left:
                    targetObj.X -= targetObj.Rete;
                    break;
                case Keys.Up:
                    targetObj.Y -= targetObj.Rete;
                    break;
                case Keys.Down:
                    targetObj.Y += targetObj.Rete;
                    break;
            }
            RePaintObj(new MyVector(x, y, targetObj.Rete, Color.Black), targetObj);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            AMove(keyData);

            //return false;//如果要调用KeyDown,这里一定要返回false才行,否则只响应重写方法里的按键 
            //这里调用一下父类方向,相当于调用普通的KeyDown事件.//所以按空格会弹出两个对话框

            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            RePaintObj(new MyVector(targetObj.X, targetObj.Y, targetObj.Rete, Color.Black),
                VectorHepler.GetNewVector(targetObj, new MyVector(e.X, e.Y, targetObj.Rete, targetObj.MyColor)));
        }

    }
}
